package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class IndexController {

    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IDestinationTemplateSearchService destinationTemplateSearchService;

    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;

    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;

    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateSearchService;


    @Reference
    private ITravelService travelService;


    @Reference
    private ISearchService searchService;



    @RequestMapping("")
    public String index(Model model) {
        //HttpServletRequest request, HttpServletResponse response
        /*//6: 用户再发起一次请求, 假设需要进行用户登录检查(需要获取用户信息), 此时可以通过request对象获取所有cookie对象
        //7:遍历所有cookie对象, 查找name为 token的cookie对象, 并通过这个cookie对象的value值(token的值), 作为key,查询redis数据库, 进而获取user对象
        String token = CookieUtil.getToken(request, response);
        UserInfo userInfo = userInfoRedisService.getUserByToken(token);
        //8:延长token在redis的时间, 30分钟, cookie存活时间也需要延长30分

        //共享用户数据
        request.getSession().setAttribute("userInfo",userInfo);
        //9: 通过user对象是否存在判断用户是否登录*/
        //tcs
        List<TravelCommend> top5 = travelCommendService.getTop5();
        model.addAttribute("tcs", top5);

        StrategyCommend sc = strategyCommendService.getTop5().get(0);
        model.addAttribute("sc", sc);
        return "index/index";
    }

    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo") TravelQuery qo) {

        model.addAttribute("pageInfo", travelService.query(qo));
        return "index/travelPageTpl";
    }

    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_TYPE_DEST://目的地
                return destSearch(model, qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY://攻略
                return strategySearch(model, qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL://游记
                return travelSearch(model, qo);
            case SearchQueryObject.SEARCH_TYPE_USER://用户
                return userSearch(model, qo);
            default:
                return allSearch(model, qo); //所有
        }
    }

    //目的地
    private String destSearch(Model model, SearchQueryObject qo) {
        SearchResultVO data = new SearchResultVO();
        //1:查询目的地对象
        DestinationTemplate dest = destinationTemplateSearchService.findByName(qo.getKeyword());
        if(dest != null){
            //2:查该目的地下的所有攻略, 前5个
            long total = 0;
            List<StrategyTemplate> sts = strategyTemplateSearchService.findByDestName(dest.getName());
            total += sts.size();
            if(sts.size() > 5){
                sts = sts.subList(0, 5);
            }
            //3:查该目的地下的所有游记, 前5个
            List<TravelTemplate> ts = travelTemplateSearchService.findByDestName(dest.getName());
            total += ts.size();
            if(ts.size() > 5){
                ts = ts.subList(0, 5);
            }
            //4:查该目的地下的所有用户, 前5个
            List<UserInfoTemplate> us = userInfoTemplateSearchService.findByDestName(dest.getName());
            total += us.size();
            if(us.size() > 5){
                us = us.subList(0, 5);
            }

            data.setStrategys(sts);
            data.setTravels(ts);
            data.setUsers(us);
            data.setTotal(total);

            model.addAttribute("data", data);
        }
        model.addAttribute("dest", dest);

        return "index/searchDest";
    }

    //用户
    private String userSearch(Model model, SearchQueryObject qo) {
        //参数1: 索引 参数2:索引类型  参数3:结果list泛型 参数4:qo  参数5:查询的字段集合
        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");

        model.addAttribute("page", page);
        return "index/searchUser";
    }

    //游记
    private String travelSearch(Model model, SearchQueryObject qo) {
        //参数1: 索引 参数2:索引类型  参数3:结果list泛型 参数4:qo  参数5:查询的字段集合
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");

        model.addAttribute("page", page);
        return "index/searchTravel";
    }

    ///攻略
    private String strategySearch(Model model, SearchQueryObject qo) {
        //参数1: 索引 参数2:索引类型  参数3:结果list泛型 参数4:qo  参数5:查询的字段集合
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle","summary");
        model.addAttribute("page", page);
        return "index/searchStrategy";

    }

    //所有
    private String allSearch(Model model, SearchQueryObject qo) {
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class, qo, "name", "info");
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle","summary");
        SearchResultVO data = new SearchResultVO();
        data.setUsers(users.getContent());
        data.setTravels(travels.getContent());
        data.setStrategys(strategys.getContent());
        data.setDests(dests.getContent());
        data.setTotal(dests.getTotalElements() + users.getTotalElements() +travels.getTotalElements() +  strategys.getTotalElements());
        model.addAttribute("data", data);
        return "index/searchAll";
    }

//----------------------------------------------------------------

    //需求: 在请求方法获取当前登录用户对象

   /* @RequestMapping("/useInfo")
    @ResponseBody
    public Object useInfo(HttpServletRequest request){
        String  token = CookieUtil.getToken(request);
        UserInfo userInfo = userInfoRedisService.getUserByToken(token);

        return userInfo;
    }*/




//    @RequestMapping("/useInfo2")
//    @ResponseBody
//    public Object useInfo2(HttpServletRequest request,UserInfo userInfo){
//
//
//        return userInfo;
//    }




    //1:springmvc也有参数解析器
    //2:自定义参数解析器

    //如果直接在映射方法中写 UserInfo userInfo 出现2个参数注解解析出现冲突
    // 会以springmvc的为主



    //如果UserInfo 参数前面贴有@UserParam注解标签使用我自己自定义参数解析器
    //没有时, 用springmvc自己的

//    @RequestMapping("/useInfo3")
//    @ResponseBody
//    public Object useInfo3(HttpServletRequest request,  UserInfo userInfo){
//        return userInfo;
//    }


}